<template>
  <a-drawer
    :title="title"
    placement="right"
    :closable="true"
    :width="width"
    :visible="visible"
    @close="handleCancel"
    destroyOnClose
  >
    <a-tabs type="card">
      <a-tab-pane key="1" tab="入库订单信息">
        <in-depository-order-info-tab ref="inDepositoryOrderInfoTab" :record="model"></in-depository-order-info-tab>
      </a-tab-pane>
      <a-tab-pane key="2" tab="订单材料信息">
        <in-depository-material-info-tab ref="inDepositoryMaterialInfoTab" :materialData="materialData" :record="model"></in-depository-material-info-tab>
      </a-tab-pane>
    </a-tabs>
  </a-drawer>
</template>

<script>
  import {httpAction, getAction, deleteAction,postAction} from '@/api/manage'
  import {validateDuplicateValue} from '@/utils/util'
  import '@/assets/less/TableExpand.less'
  import goodsRejectedOrderDetailModal from "../../purchaseGoodsRejectedOrder/modules/goodsRejectedOrderDetailModal";
  import inDepositoryOrderInfoTab from "./tab/inDepositoryOrderInfoTab";
  import inDepositoryMaterialInfoTab from "./tab/inDepositoryMaterialInfoTab";

  export default {
    name: 'purchaseOrderDetailModal',
    components: {
      goodsRejectedOrderDetailModal,
      inDepositoryOrderInfoTab,
      inDepositoryMaterialInfoTab
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },

    data() {
      return {
        materialData: [],
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: '3%',
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '名称',
            dataIndex: 'materialName',
            width: '13%',
            align: "center",
            scopedSlots: {customRender: 'materialName'},
          },
          {
            title: '型号',
            dataIndex: 'modelNumber',
            width: '12%',
            align: "center",
            scopedSlots: {customRender: 'modelNumber'},
          },
          {
            title: '规格',
            dataIndex: 'specifications',
            width: '13%',
            align: "center",
            scopedSlots: {customRender: 'specifications'},
          },
          {
            title: '品牌',
            dataIndex: 'brand',
            width: '12%',
            align: "center",
            scopedSlots: {customRender: 'brand'},
          },
          {
            title: '单位',
            dataIndex: 'unit',
            width: '5%',
            align: "center",
          },
          {
            title: '市场价',
            dataIndex: 'marketPrice',
            width: '5%',
            align: "center",
            scopedSlots: {customRender: 'marketPrice'},
          },
          {
            title: '预算价',
            dataIndex: 'estimatedCost',
            width: '5%',
            align: "center",
            scopedSlots: {customRender: 'estimatedCost'},
          },
          {
            title: '成本价',
            dataIndex: 'costPrice',
            width: '5%',
            align: "center",
          },
          {
            title: '单价',
            dataIndex: 'unitPrice',
            width: '5%',
            align: "center",
            scopedSlots: {customRender: 'unitPrice'},
          },
          {
            title: '数量',
            dataIndex: 'materialNum',
            width: '5%',
            align: "center",
            scopedSlots: {customRender: 'materialNum'},
          },
          {
            title: '折扣率(%)',
            dataIndex: 'discountRate',
            width: '7%',
            align: "center",
            scopedSlots: {customRender: 'discountRate'},
          },
          {
            title: '折扣额',
            dataIndex: 'discountMoney',
            width: '7%',
            align: "center",
            scopedSlots: {customRender: 'discountMoney'},
          },
          {
            title: '折后单价',
            dataIndex: 'discountUnitPrice',
            width: '7%',
            align: "center",
            scopedSlots: {customRender: 'discountUnitPrice'},
          },
          {
            title: '小计金额',
            dataIndex: 'subtotal',
            width: '5%',
            align: "center",
            scopedSlots: {customRender: 'subtotal'},
          },
          {
            title: '备注',
            dataIndex: 'remark',
            width: '12%',
            align: "center",
            scopedSlots: {customRender: 'remark'},
          },
        ],
        title: '',
        width: '100%',
        visible: false,
        disableSubmit: false,
        fullscreen: true,
        model: {},
        statusValue:"",
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        url: {
          queryInDepositoryOrderItemList: "/ghSupplychainPurchaseOrderItem/list",
          createGoodsRejectedOrderUrl: "/ghSupplychainPurchaseOrder/createGoodsRejectedOrder",
          queryPurchaseByOrderNo:"/ghSupplychainPurchaseOrder/queryPurchaseByOrderNo",
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created() {

    },
    methods: {
      relevanceGoodsRejectedNoDetail(orderNo){
        getAction(this.url.queryPurchaseByOrderNo, {orderNo: orderNo}).then((res) => {
          if (res.success) {
            var orderObj = res.result;
            if (orderObj == null){
              this.$message.error("暂无该订单的信息");
              return;
            }else {
              this.$refs.goodsRejectedOrderDetailModal.title = "采购退货订单详情";
              this.$refs.goodsRejectedOrderDetailModal.showDetail(orderObj);
            }
          }
        })
      },
      //生成退货单
      createGoodsRejected(){
        var that = this;
        this.model.materialPurchaseOrderItemVos = this.materialData;
        postAction(this.url.createGoodsRejectedOrderUrl,this.model).then(res=>{
          if (res.success) {
            this.$message.success(res.message);
            that.$emit('ok');
          }else {
            this.$message.error(res.message);
          }
        })
      },
      getOrderStatusValue(status){
        switch (status) {
          case 1:
            this.statusValue = "草稿"
            return "草稿"
          case 2:
            this.statusValue = "审批中"
            return "审批中"
          case 3:
            this.statusValue = "审批通过"
            return "审批通过"
          case 4:
            this.statusValue = "未通过"
            return "未通过"
          case 5:
            this.statusValue = "已撤销"
            return "已撤销"
        }
      },
      showDetail(record) {
        this.getOrderStatusValue(record.orderStatus)
        this.model = Object.assign({}, record);
        this.model.statusValue = this.statusValue;
        getAction(this.url.queryInDepositoryOrderItemList, {id: record.id}).then((res) => {
          if (res.success) {
            this.materialData = res.result.records || res.result;
          }
        })
        this.visible = true;
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>
<style scoped>
  .editable-row-operations a {
    margin-right: 8px;
  }

  .tableHiddle {
    display: none;
  }
</style>